package com.demo2do.fms.admin.entity.report.supplier;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.WeakHashMap;

import org.springframework.jdbc.core.RowMapper;

import com.demo2do.core.persistence.support.QueryBuilder;
import com.demo2do.core.persistence.support.QueryFilter;
import com.demo2do.core.persistence.support.QueryOrder;

/**
 *
 * @author Ark
 *
 */
public class TractorReportSearch implements QueryBuilder {

    /** 所属供方 */
    private Long supplierId;
    
    /** 车辆审核状态 */
    private String status;
    
    private QueryOrder queryOrder;
    
    private QueryFilter queryFilter;
    
    /**
     * The default constructor
     */
    public TractorReportSearch() {
        queryOrder = new QueryOrder("t.brand_cn_code", "ASC");
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getPreparedFilter(boolean)
     */
    public QueryFilter getPreparedFilter(boolean refresh) {
        
        if (queryFilter == null || refresh) {
            
            queryFilter = new QueryFilter().addEquals("s", "id", supplierId)
                                           .addEquals("t", "status", status);
            
        }
        
        return queryFilter;
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildSentenceTemplate()
     */
    public String buildSentenceTemplate() {
        
        StringBuffer buffer = new StringBuffer(512);
        
        buffer.append("SELECT t.id AS id, ")
                     .append("t.brand_cn_code AS brandCnCode, ")
                     .append("t.brand_en_code AS brandEnCode, ")
                     .append("t.floor_lord AS floorLord, ")
                     .append("t.vin AS vincode, ")
                     .append("t.brand AS brand, ")
                     .append("t.code AS code, ")
                     .append("t.is_synchronize AS synchronize, ")
                     .append("s.name AS supplierName, ")
                     .append("s.short_name AS supplierSimpleName, ")
                     .append("CASE WHEN t.car_ownership = 'OWN' THEN '自有' WHEN t.car_ownership = 'HOLDING' THEN '控股' WHEN t.car_ownership = 'NOTCONVERTED' THEN '未转籍' WHEN t.car_ownership = 'LEASE' THEN '租赁' WHEN t.car_ownership = 'PERSONALANCHORS' THEN '个人挂靠' WHEN t.car_ownership = 'COMPANYANCHORS' THEN '公司挂靠' END AS carOwnership, ")
                     .append("CASE WHEN t.trans_mean = 'GOODSVEHICLES' THEN '商品车运输车辆' WHEN t.trans_mean = 'FLAT' THEN '平板/栏板车' WHEN t.trans_mean = 'WAREHOUSE' THEN '仓储内驳车' WHEN t.trans_mean = 'RESCUE' THEN '救援车' END AS transMean, ")
                     .append("t.gps AS gps, ")
                     .append("t.sim_card AS simCard, ")
                     .append("CASE WHEN t.status = 'EDITING' THEN '编辑中' WHEN t.status = 'AUDITING' THEN '待审核' WHEN t.status = 'REJECTED' THEN '驳回' WHEN t.status = 'QUALIFIED' THEN '合格' WHEN t.status = 'MODIFIED' THEN '变更申报' END AS status, ")
                     .append("t.reject_reason AS rejectReason, ")
                     .append("t.declare_time AS declareTime, ")
                     .append("t.issuing_time AS issuingTime, ")
                     .append("t.registration_time AS registrationTime, ")
                     .append("t.annual_inspection_date AS annualInspectionDate,")
                     .append("t.horsepower AS horsepower, ")
                     .append("t.axles as axles, ")
                     .append("CASE WHEN t.vehicle_type = 'HEAVY_SEMITRAILER' THEN '重型半挂牵引车' WHEN t.vehicle_type = 'HEAVY_SPECIAL_TRUCK' THEN '重型特殊结构货车' WHEN t.vehicle_type = 'MEDIUM_NON_CARGO' THEN '中型非载货专项作业车' WHEN t.vehicle_type = 'HEAVY_TRANSPORT_TRACTOR' THEN '重型车辆运输车' END AS vehicleType, ")
                     .append("t.trainload as trainload, ")
                     .append("t.is_bind AS bind ")
              .append("FROM fms_tractor t, fms_supplier s ")
              .append("WHERE t.is_disabled = 0 AND s.is_disabled = 0 AND t.supplier_id = s.id")
              .append(getPreparedFilter(false).clauseSentence() + queryOrder.sentence());
        
        return buffer.toString();
        
    }
    
    /**
     * build rowMapper
     * 
     * @return
     */
    public RowMapper<Map<String, Object>> buildRowMapper() {
        
        RowMapper<Map<String, Object>> rowMapper = new RowMapper<Map<String,Object>>() {

            /* (non-Javadoc)
             * @see org.springframework.jdbc.core.RowMapper#mapRow(java.sql.ResultSet, int)
             */
            public Map<String, Object> mapRow(ResultSet rs, int rowNum) throws SQLException {
                
                Map<String, Object> result = new WeakHashMap<String, Object>();
                
                result.put("id", rs.getObject("id"));
                result.put("brandCnCode", rs.getObject("brandCnCode"));
                result.put("brandEnCode", rs.getObject("brandEnCode"));
                result.put("floorLord", rs.getObject("floorLord"));
                result.put("vincode", rs.getObject("vincode"));
                result.put("brand", rs.getObject("brand"));
                result.put("code", rs.getObject("code"));
                result.put("synchronize", rs.getBoolean("synchronize"));
                result.put("supplierName", rs.getObject("supplierName"));
                result.put("supplierSimpleName", rs.getObject("supplierSimpleName"));
                result.put("carOwnership", rs.getObject("carOwnership"));
                result.put("transMean", rs.getObject("transMean"));
                result.put("gps", rs.getObject("gps"));
                result.put("simCard", rs.getObject("simCard"));
                result.put("status", rs.getObject("status"));
                result.put("rejectReason", rs.getObject("rejectReason"));
                result.put("declareTime", rs.getObject("declareTime"));
                result.put("issuingTime", rs.getObject("issuingTime"));
                result.put("registrationTime", rs.getObject("registrationTime"));
                result.put("horsepower", rs.getObject("horsepower"));
                result.put("axles", rs.getObject("axles"));
                result.put("vehicleType", rs.getObject("vehicleType"));
                result.put("trainload", rs.getObject("trainload"));
                result.put("bind", rs.getBoolean("bind"));
                result.put("annualInspectionDate",rs.getObject("annualInspectionDate"));
                
                return result;
                
            }
            
        };
        
        return rowMapper;
        
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#buildParameters()
     */
    public Map<String, Object> buildParameters() {
        return getPreparedFilter(false).parameters();
    }
    
    /* (non-Javadoc)
     * @see com.demo2do.core.persistence.support.QueryBuilder#getOrder()
     */
    public QueryOrder getOrder() {
        return queryOrder;
    }

    /**
     * @return the supplierId
     */
    public Long getSupplierId() {
        return supplierId;
    }

    /**
     * @return the status
     */
    public String getStatus() {
        return status;
    }

    /**
     * @param supplierId the supplierId to set
     */
    public void setSupplierId(Long supplierId) {
        this.supplierId = supplierId;
    }

    /**
     * @param status the status to set
     */
    public void setStatus(String status) {
        this.status = status;
    }
    
}
